<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运营商" prop="operatorUserId" v-if="getoperatorfunc(this).userType != 1">
        <el-select v-model="queryParams.operatorUserId" placeholder="运营商" filterable clearable style="width: 200px">
          <el-option
            v-for="dict in operatorList"
            :key="dict.id"
            :label="dict.label"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list">
      <el-table-column label="订单号" align="center" prop="orderCode" width="150" fixed="left"/>
      <el-table-column label="运营商名称" align="center" prop="operatorName" v-if="getoperatorfunc(this).userType != 1" width="150"/>
      <el-table-column label="租赁天数" align="center" prop="rentDays" width="150"/>
      <el-table-column label="收件人名称" align="center" prop="userName" width="150"/>
      <el-table-column label="收件人手机" align="center" prop="userPhone" width="150"/>
      <el-table-column label="订单完成时间" align="center" prop="finishTime" width="150"/>
      <el-table-column label="订单取消时间" align="center" prop="cancelTime" width="150"/>
      <el-table-column label="支付时间" align="center" prop="payTime" width="150"/>
      <el-table-column label="押金" align="center" prop="deposit" width="150"/>
      <el-table-column label="租金" align="center" prop="rent" width="150"/>
      <el-table-column label="平台佣金" align="center" prop="platformBrokeragePrice" width="150"/>
      <el-table-column label="运营商佣金" align="center" prop="operatorBrokeragePrice" width="150"/>
      <el-table-column label="租赁原价" align="center" prop="totalPrice" width="150"/>
      <el-table-column label="应付金额" align="center" prop="payPrice" width="150"/>
      <el-table-column label="订单状态" align="center" prop="orderStatus" width="150">
        <template slot-scope="{row}">
          <dict-tag :options="dict.type.server_order_status" :value="row.orderStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="150" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150px" fixed="right">
        <template slot-scope="{row}">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(row)"
            v-hasPermi="['trade:rentOrder:query']"
          >查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="90%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row class="diy-text-sm diy-leading-10" :gutter="10" v-if="title == '查看详情'">
          <el-col :span="item.span || 12" v-for="(item,index) in formDetail" :key="index">
            <div class="flex_hcneter">
              <svg-icon icon-class="dot" class-name='diy-text-primary diy-mr-1'/>
              <div v-if="item.zidian && item.isCustom" class="flex_hcneter">
                {{item.label}}：<dict-tag :options="dict.type[item.zidian]" :value="form[item.prop]"/>
              </div>
              <div v-else-if="item.type == 'textarea' && item.isCustom">
                {{item.label}}：
                <div v-html="form[item.prop]"></div>
              </div>
              <div v-else-if="item.type == 'image' && item.isCustom" class="flex_hcneter" style="margin-bottom:10px;">
                {{item.label}}：
                <el-image v-if="form[item.prop]"
                  style="width: 100px; height: 100px;"
                  :src="form[item.prop]"
                  :preview-src-list="[form[item.prop]]">
                </el-image>
              </div>
              <div v-else-if="item.type == 'list'" style="margin-bottom:10px;width:100%;">
                <h2>{{item.label}}</h2>
                <div v-for="(item1,index1) in form[item.prop]" :key="index1">
                  <el-col :span="12" ><svg-icon icon-class="dot" class-name='diy-text-primary diy-mr-1'/>租赁 SPU：{{ item1.spuName || ''}}</el-col>
                  <el-col :span="12" ><svg-icon icon-class="dot" class-name='diy-text-primary diy-mr-1'/>租赁天数：{{ item1.rentDays || ''}}</el-col>
                  <el-col :span="24" class="flex_hcneter">
                    <svg-icon icon-class="dot" class-name='diy-text-primary diy-mr-1'/>租赁图片：
                    <div  style="margin-bottom:10px;">
                      <el-image v-if="item1.picUrl"
                        style="width: 100px; height: 100px;"
                        :src="item1.picUrl"
                        :preview-src-list="[item1.picUrl]">
                      </el-image>
                    </div>
                  </el-col>
                  <el-col :span="12" ><svg-icon icon-class="dot" class-name='diy-text-primary diy-mr-1'/>子订单实付金额：{{ item1.payPrice || ''}}</el-col>
                  <el-col :span="12" ><svg-icon icon-class="dot" class-name='diy-text-primary diy-mr-1'/>押金：{{ item1.deposit || ''}}</el-col>
                  <el-col :span="12" ><svg-icon icon-class="dot" class-name='diy-text-primary diy-mr-1'/>租金：{{ item1.rent || ''}}</el-col>
                </div>
                
              </div>
              <div v-else>{{item.label}}：{{ form[item.prop] }}{{ item.danwei || '' }}</div>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRentOrder, getRentOrder, } from "@/api/commodity/leaseOrder";
import { listAllOperator } from "@/api/operator/operatorList";
export default {
  name: "SrviceOrder",
  dicts: ['server_order_status', 'comm_yesno_status','pay_status_options','server_order_cancel_type','pay_channel_code',],
  components: { },
  data() {
    return {
      formDetail:[//表单
        {label:'订单号',prop:'orderCode',},{label:'收件人名称',prop:'userName',},{label:'收件人手机',prop:'userPhone',},
        {label:'租赁天数',prop:'rentDays',},
        {label:'押金',prop:'deposit',},{label:'租金',prop:'rent',},{label:'退款金额',prop:'refundPrice',},
        {label:'平台佣金',prop:'platformBrokeragePrice',},{label:'运营商佣金',prop:'operatorBrokeragePrice',},{label:'租赁原价',prop:'totalPrice',},
        {label:'应付金额',prop:'payPrice',},{label:'经度',prop:'addressLongitude',},{label:'纬度',prop:'addressLatitude',},
        {label:'省',prop:'addressProvinceName',},{label:'市',prop:'addressCityName',},{label:'区',prop:'addressCountyName',},{label:'街道',prop:'addressTownName',},
        {label:'详细地址',prop:'addressDetail',span:24,},
        {label:'订单完成时间',prop:'finishTime',},{label:'订单取消时间',prop:'cancelTime',},{label:'评价时间',prop:'commentTime',},
        {label:'支付时间',prop:'payTime',},{label:'订单取消时间',prop:'cancelTime',},
        {label:'支付渠道',prop:'payChannelCode',zidian:'pay_channel_code',isCustom:true,},
        {label:'支付状态',prop:'payStatus',zidian:'pay_status_options',isCustom:true,},
        {label:'订单状态',prop:'orderStatus',zidian:'server_order_status',isCustom:true,},
        {label:'取消类型',prop:'cancelType',zidian:'server_order_cancel_type',isCustom:true,},
        {label:'订单备注',prop:'remark',span:24,type:'textarea',},{label:'创建时间',prop:'createTime',},
        {label:'租赁订单明细',prop:'items',span:24,type:'list',},
      ],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,operatorUserId: undefined,
        orderCode: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      operatorList:[],//运营商数据
    };
  },
  created() {
    this.listAllOperator()
    this.getList();
  },
  methods: {
    async listAllOperator(){//获取运营商
      let res =await listAllOperator()
      let data = res.data || []
      this.operatorList = data
    },
    /** 查列表 */
    getList() {
      this.loading = true;
      listRentOrder(this.queryParams).then(response => {
        let rows = response.rows || [];
        this.list = rows.map(item=>{
          return {
            ...item,
            ...this.getpricezh(item),
          }
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleDetail(row) {
      const id = row.id
      getRentOrder(id).then(response => {
        let data =  response.data || {};
        let items = data.items || []
        this.form = {
          ...data,
          ...this.getpricezh(data),
          items:items.map(item=>{
            return {
              ...item,
              deposit:this.math.divide(item.deposit || 0,100),
              rent:this.math.divide(item.rent || 0,100),
              payPrice:this.math.divide(item.payPrice || 0,100),
            }
          })
        }
        this.open = true;
        this.title = "查看详情";
      });
    },
    getpricezh(item){//金额转换
      return {
        totalPrice:this.math.divide(item.totalPrice || 0,100),
        payPrice:this.math.divide(item.payPrice || 0,100),
        refundPrice:this.math.divide(item.refundPrice || 0,100),
        platformBrokeragePrice:this.math.divide(item.platformBrokeragePrice || 0,100),
        operatorBrokeragePrice:this.math.divide(item.operatorBrokeragePrice || 0,100),
        deposit:this.math.divide(item.deposit || 0,100),
        rent:this.math.divide(item.rent || 0,100),
      }
    },
  }
};
</script>